package dao;

import java.util.Date;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

import pojo.Product;

/**
 * A data access object (DAO) providing persistence and search support for
 * Product entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see pojo.Product
 * @author MyEclipse Persistence Tools
 */

public class ProductDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(ProductDAO.class);
	// property constants
	public static final String SORT = "sort";
	public static final String VERSION = "version";
	public static final String SIZE = "size";
	public static final String LANGUAGE = "language";
	public static final String COMPANY = "company";
	public static final String REQUIREMENTS = "requirements";
	public static final String SUMMARY = "summary";
	public static final String NAME = "name";
	public static final String THUMBNAIL = "thumbnail";
	public static final String ADAPTION = "adaption";
	public static final String FILELINK = "filelink";
	public static final String WEIGHT = "weight";
	public static final String ITUNES_LINK = "itunesLink";

	public void save(Product transientInstance) {
		log.debug("saving Product instance");
		try {
			Transaction tx=getSession().beginTransaction(); 
			getSession().save(transientInstance);
			tx.commit();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Product persistentInstance) {
		log.debug("deleting Product instance");
		try {
			Transaction tx=getSession().beginTransaction(); 
			getSession().delete(persistentInstance);
			tx.commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Product findById(java.lang.Integer id) {
		log.debug("getting Product instance with id: " + id);
		try {
			Product instance = (Product) getSession().get("pojo.Product", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Product instance) {
		log.debug("finding Product instance by example");
		try {
			List results = getSession().createCriteria("pojo.Product").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Product instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Product as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public List findAllOrderByWeight() {
			log.debug("finding all Product instances ");
			try {
				String queryString = "from Product as model order by model.weight desc";
				Query queryObject = getSession().createQuery(queryString);
				return queryObject.list();
			} catch (RuntimeException re) {
				log.error("find all failed", re);
				throw re;
			}
		}
	
	public List findBySort(Object sort) {
		return findByProperty(SORT, sort);
	}

	public List findByVersion(Object version) {
		return findByProperty(VERSION, version);
	}

	public List findBySize(Object size) {
		return findByProperty(SIZE, size);
	}

	public List findByLanguage(Object language) {
		return findByProperty(LANGUAGE, language);
	}

	public List findByCompany(Object company) {
		return findByProperty(COMPANY, company);
	}

	public List findByRequirements(Object requirements) {
		return findByProperty(REQUIREMENTS, requirements);
	}

	public List findBySummary(Object summary) {
		return findByProperty(SUMMARY, summary);
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByThumbnail(Object thumbnail) {
		return findByProperty(THUMBNAIL, thumbnail);
	}

	public List findByAdaption(Object adaption) {
		return findByProperty(ADAPTION, adaption);
	}

	public List findByFilelink(Object filelink) {
		return findByProperty(FILELINK, filelink);
	}
	public List findByWeight(Object weight) {
		return findByProperty(WEIGHT, weight);
	}
	public List findByItunesLink(Object itunesLink) {
		return findByProperty(ITUNES_LINK, itunesLink);
	}
	public List findAll() {
		log.debug("finding all Product instances");
		try {
			String queryString = "from Product";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Product merge(Product detachedInstance) {
		log.debug("merging Product instance");
		try {
			Transaction tx=getSession().beginTransaction(); 
			Product result = (Product) getSession().merge(detachedInstance);
			tx.commit();
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Product instance) {
		log.debug("attaching dirty Product instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Product instance) {
		log.debug("attaching clean Product instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}